package com.example.dishfo.myapplication.activity.signup;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.example.dishfo.myapplication.R;
import com.example.dishfo.myapplication.activity.BaseActivity;
import com.example.dishfo.myapplication.activity.userlogin.UserViewModel;
import com.example.dishfo.myapplication.customview.mydialog.WaitDialogFragment;
import com.example.dishfo.myapplication.customview.toolbar.TextTitle;
import com.example.dishfo.myapplication.network.resbean.HttpBean;
import com.example.dishfo.myapplication.util.ActivityUtil;
import com.example.dishfo.myapplication.util.JsonUtil;
import com.google.gson.JsonElement;

import java.util.Objects;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;

public class SignUpActivity extends BaseActivity {

    @BindView(R.id.textInputLayout_email) TextInputLayout textInputLayoutEmail;
    @BindView(R.id.textInputLayout_name) TextInputLayout textInputLayoutName;
    @BindView(R.id.textInputLayout_pwd) TextInputLayout textInputLayoutPwd;
    @BindView(R.id.btn_commit) Button buttonCommit;
    @BindView(R.id.textView_res) TextView textViewRes;
    @BindView(R.id.toolbar) TextTitle textTitle;

    @BindString(R.string.signup_wait) String signupWait;

    private WaitDialogFragment fragment;
    private boolean isWait=false;
    private UserViewModel viewModel;
    private LiveData<HttpBean> signupRes;

    private View.OnClickListener clickListenerCommit=v -> {
        ActivityUtil.clearTextInputError(textInputLayoutEmail);
        ActivityUtil.clearTextInputError(textInputLayoutName);
        ActivityUtil.clearTextInputError(textInputLayoutPwd);
        if(showError()){
            return;
        }

        String email=ActivityUtil.getEditText(textInputLayoutEmail).toString();
        String name=ActivityUtil.getEditText(textInputLayoutName).toString();
        String pwd=ActivityUtil.getEditText(textInputLayoutPwd).toString();

        viewModel.signUp(name,pwd,email);
        startWait();
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityUtil.setTransportStatusBar(getWindow());
        setContentView(R.layout.activity_sign_up);
        ButterKnife.bind(this);

        fragment=WaitDialogFragment.getInstance(signupWait,dialog -> {
            if(isWait){
                isWait=false;
                viewModel.cancelSignUp();
            }
        });
        viewModel= ViewModelProviders.of(this).get(UserViewModel.class);
        signupRes=viewModel.getSignUpRes();

        signupRes.observe(this,httpBean -> {
            stopWait();
            assert httpBean != null;
            JsonElement element=JsonUtil.getFromJson("result",httpBean.getForm());
            textViewRes.setText(element==null?"未知错误":element.getAsString());
        });

        buttonCommit.setOnClickListener(clickListenerCommit);
        textTitle.setOnClickListener(v -> onBackPressed());
    }

    private void stopWait(){
        if(isWait){
            isWait=false;
            fragment.dismiss();
        }
    }

    private void startWait(){
        if(!isWait){
            isWait=true;
            fragment.show(getSupportFragmentManager(),"wait");
        }
    }

    private boolean showError(){
        String email= ActivityUtil.getEditText(textInputLayoutEmail).toString();
        String user=ActivityUtil.getEditText(textInputLayoutName).toString();
        String pwd=ActivityUtil.getEditText(textInputLayoutPwd).toString();

        if(TextUtils.isEmpty(email)){
            textInputLayoutEmail.setError("邮箱地址为空");
            return true;
        }else  if (TextUtils.isEmpty(user)){
            textInputLayoutName.setError("用户名为空");
            return true;
        }else if(TextUtils.isEmpty(pwd)){
            textInputLayoutPwd.setError("密码为空");
            return true;
        }
        return false;
    }

}
